package com.bxs.dao.email;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bxs.dao.BaseDataHandler;
import com.bxs.global.LogLevel;
import com.bxs.mapper.MailReceiverMapper;
import com.bxs.message.MessageUtils;
import com.bxs.pojo.DangerMsg;
import com.bxs.pojo.MailReceiver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 邮件处理器，将处理结果发送到指定邮箱
 */
@Slf4j
@Service
public class EmailHandler extends BaseDataHandler {
    @Autowired
    MessageUtils messageUtils;

    @Autowired
    MailReceiverMapper mailReceiverMapper;

    @Override
    public void handleData(DangerMsg data) {
        if(data.getCode() != LogLevel.VERY_DANGER.getCode())return;
//        System.out.println("EmailHandler");
        List<MailReceiver> mailReceivers = mailReceiverMapper.selectList(new QueryWrapper<>());
        for(MailReceiver mailReceiver : mailReceivers){
            try{
                messageUtils.sendSimpleEmail("IDS 日志警告", data.getDesc(), "bikexs@foxmail.com", mailReceiver.getAddress());
            }catch (Exception e){
                log.error("mail send error:", e);
            }
        }
    }
}
